package com.hsj.common.system.controller;

import com.hsj.common.system.convert.SysMenuConvert;
import com.hsj.common.system.service.ISysMenuService;
import com.hsj.common.system.vo.request.ReqSysMenuPageQueryVo;
import com.hsj.common.system.vo.request.ReqSysMenuWithChildrenVo;
import com.hsj.common.system.vo.response.RespSysMenuVo;
import com.hsj.common.system.vo.response.RespSysMenuWithChildrenVo;
import com.hsj.common.system.vo.response.RespUserPermissionVo;
import com.hsj.common.security.SecurityContext;
import com.hsj.common.system.vo.request.ReqBatchDelSysMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;


/**
 * 系统菜单 Controller
 * @Author: Czy
 * @Date: 2024-01-23 23:03:26
 * @Email: 314170122@qq.com
 */
@Api(tags = "系统菜单管理")
@RestController
@RequestMapping("/sysMenu")
@Slf4j
public class SysMenuController {

    private final ISysMenuService sysMenuService;
    private final SysMenuConvert sysMenuConvert;

    public SysMenuController(ISysMenuService sysMenuService, SysMenuConvert sysMenuConvert) {
        this.sysMenuService = sysMenuService;
        this.sysMenuConvert = sysMenuConvert;
    }

    @PostMapping("/createSysMenu")
    @ApiOperation(value = "创建系统菜单", notes = "创建系统菜单", httpMethod = "POST")
    public Serializable createSysMenu(@Valid @RequestBody ReqSysMenuWithChildrenVo vo){
        return sysMenuService.editSysMenu(vo);
    }

    @PostMapping("/updateSysMenu")
    @ApiOperation("修改系统菜单")
    public Serializable updateSysMenu(@Valid @RequestBody ReqSysMenuWithChildrenVo vo){
        return sysMenuService.editSysMenu(vo);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除系统菜单")
    public Boolean delete(@PathVariable("id")String id){
        return sysMenuService.delete(id) == 1L;
    }


    @PostMapping("/batchDel")
    @ApiOperation("修改系统菜单")
    public Integer batchDel(@Valid @RequestBody ReqBatchDelSysMenuVo vo){
        return sysMenuService.batchDelSysMenu(vo);
    }

    @PostMapping("/loadMenu")
    @ApiOperation("分页查询系统菜单")
    public List<RespSysMenuWithChildrenVo> pageList(@Valid @RequestBody ReqSysMenuPageQueryVo vo){
        return sysMenuService.loadAllMenu();
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取系统菜单信息")
    public RespSysMenuVo get(@PathVariable("id")String id) {
        return sysMenuConvert.doToVo(sysMenuService.get(id));
    }

    @PostMapping("/loadMenuList")
    @ApiOperation("分页查询系统菜单")
    public RespUserPermissionVo loadMenuList(){
        return sysMenuService.loadMenuByUserId(SecurityContext.getUserId());
    }
}
